﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Input;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Media.Media3D;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class ITXABPLAN 
    {
        #region-------------Properties-----------------------
        ITXEXRPT ITXEXRPT;
        DataSet setDISPO;
        double FLeng = 0, GLeng = 0, WLeng = 0;
        int FLeadTime = 0, GLeadTime = 0, WLeadTime = 0, YLeadTime = 14;
        double Fnum = 0, Gnum = 0, Wnum = 0;
        string FEnd = "", GEnd = "", WEnd = "", YEnd = "";
        string DISPONUM = "";
        DataRow lastrow = null;
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetDetail
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetDetail");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        ITXABPLANData m_Data;
        string UserID = "";
        string WTYPE
        {
            get
            {
                if (cbWTYE.Text == "生产单")
                    return "V";
                if (cbWTYE.Text == "销售单")
                    return "K";
                if (cbWTYE.Text == "发货单")
                    return "L";
                return "V";
            }
        }
        CollectionViewSource vwWASTAMM
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwWASTAMM");
            }
        }
        DataRow dwWASTAMM
        {
            get
            {
                if (vwWASTAMM.View == null)
                    return null;
                if ((DataRowView)vwWASTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWASTAMM.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if ((DataRowView)vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        string KRS
        {
            get
            {
                return Helper.Left(txtVKPAUFKRS.Text.Trim() + "  ", 2);
            }
            set
            {
                txtVKPAUFKRS.Text = Helper.Left(txtVKPAUFKRS.Text.Trim() + value, 2);
            }
        }
        string LFD
        {
            get
            {
                return Helper.Right("000000" + txtVKPAUFLFD.Text.Trim(), 6);
            }
            set
            {
                txtVKPAUFLFD.Text = Helper.Right("000000" + value, 6); ;
            }
        }
        string POS
        {
            get
            {
                return Helper.Right("000" + txtVKPAUFPOS.Text.Trim(), 3);
            }
            set
            {
                txtVKPAUFPOS.Text = Helper.Right("000" + value, 3);
            }
        }

        public ITXABPLAN()
        {
            InitializeComponent();
            this.Name = "查询工具";
        }
        #endregion
        #region Events
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                InitializeComponent();
                m_Data = new ITXABPLANData();
                ITXEXRPT = new ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
                KRS = "JS";
                LFD = "000007";
                POS = "001";
                cbWTYE.Text = "生产单";
                ReloadCon();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion


        private void ReloadCon()
        {
            DataSet SResult = null;
            SResult = m_Data.LoadEnv();
            SetEnv.Merge(SResult);
        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {

            DataSet SResult = new DataSet();
            SResult = m_Data.LoadData(WTYPE, KRS, LFD);
            SetData.Merge(SResult);
            SetUP3D();
        }
        private void btnRefreshStamm_Click(object sender, RoutedEventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnRefreshStock_Click(object sender, RoutedEventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRefreshGS07_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetData.Clear();
                SetData.Merge(m_Data.CheckGSDAT());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRefreshWS05_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetData.Clear();
                SetData.Merge(m_Data.CheckWASTAMM());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            SetData.Clear();
        }
        private void btnDeleteABON_Click(object sender, RoutedEventArgs e)
        {
            //m_Data.DeleteABON(KRS, LFD, POS);
            m_Data.DeleteAV95(KRS, LFD, POS);
        }
        private void btnDeleteAB_Click(object sender, RoutedEventArgs e)
        {
            m_Data.Delete("00", KRS, LFD, POS);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.DocType = "ABCOMPARE";
            ITXEXRPT.strExcelFile = @"ITX\ITXABDOC.xls";
            string filename = (string)ITXEXRPT.WriteExcelReport();
        }
        RotateTransform3D rt3dud;
        RotateTransform3D rt3dlr;
        TranslateTransform3D tl3d;
        double updown = 0;
        double leftright = 0;
        int translate = 0;
        private void SetUP3D()
        {
          
            DataGrid[] dgs = { ucontrol.left, ucontrol.right, ucontrol.top, 
                                 ucontrol.bottom, ucontrol.center, ucontrol.other };
            for (int i = 0; i < 6; i++)
            {
                DataTable dt =  SetData.ABPOS; 
                dgs[i].ItemsSource = dt.DefaultView;
            }  
            rt3dud = new RotateTransform3D(AxisAngleRotation3D.Identity);
            rt3dlr = new RotateTransform3D(AxisAngleRotation3D.Identity);
            tl3d = new TranslateTransform3D(new Vector3D(0, 0, 0));
            Transform3DGroup t3dg = new Transform3DGroup();
            t3dg.Children.Add(rt3dud);
            t3dg.Children.Add(rt3dlr);
            t3dg.Children.Add(tl3d);
            v23.Transform = t3dg;
        }
        protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
        {
            e.Handled = true;
            tl3d.OffsetZ = e.Delta < 0 ? --translate * 0.05 : ++translate * 0.05;
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Up:
                    updown -= 5;
                    rt3dud.Rotation = new AxisAngleRotation3D(new Vector3D(1, 0, 0), updown);
                    break;
                case Key.Down:
                    updown += 5;
                    rt3dud.Rotation = new AxisAngleRotation3D(new Vector3D(1, 0, 0), updown);
                    break;
                case Key.Left:
                    leftright -= 5;
                    rt3dlr.Rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), leftright);
                    break;
                case Key.Right:
                    leftright += 5;
                    rt3dlr.Rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), leftright);
                    break;
                default:
                    return;
            }
        }

    }
}
